package com.bingo.generate.comm.entity;

import lombok.Data;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.function.BiConsumer;

/**
 * ClassName
 * Description
 * Create by lc
 * Date 2021/7/22 15:25
 */
@Data
public class Clock {

    //  开始时间
    private LocalDateTime startTime;

    //  持续工作时间
    private long workTime=1000*60*15;
    //  停止时间
    private long stopTime=1000*60*3;

    //  持续个数
    private int count=5;

    //  上次触发事件的毫秒数
    private long alertTime=0;

    public static long toMill(LocalDateTime ldt){
        /*//获取秒数
        Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        //获取毫秒数
        Long milliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();*/
        Long milliSecond = ldt.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return milliSecond;
    }



    public long endMill(){
        return toMill(startTime)+(workTime+stopTime)*count;
    }

    public Runnable remind=()-> {
        System.out.println("请注意休息"+LocalDateTime.now());
    };

    /**
     * type : 1 工作中    2 休息中   3 超时
     */
    public BiConsumer<Integer,Long> statusControl=(type, diff)-> {


        /*//  单位标准
        long clockUnit=(getWorkTime()+getStopTime());
        //  计算已经使用了的番茄
        long useTime=Clock.toMill(LocalDateTime.now())-Clock.toMill(startTime);
        //  计算已经使用的番茄数量
        long useCount=useTime/clockUnit;
        long currentHaveTime=useTime-(useCount*clockUnit);
        if(type==1){
            long time=(workTime-currentHaveTime)/1000;
            if(time%10==0){
                remindMin.run();
            }
            return;
        }*/
    };

    //  分钟提醒
    public Runnable remindMin=()-> {
        System.out.println("分钟提醒：,请注意"+LocalDateTime.now());
    };

    public Runnable timeOut=()-> {
        System.out.println("请注意休息"+LocalDateTime.now());
    };

    public String print1() {
        long endMill=this.endMill();
        long diff=endMill-Clock.toMill(LocalDateTime.now());
        if(diff<0){
            System.out.println("当前时间:"+LocalDateTime.now());
            if((System.currentTimeMillis()-alertTime)>10*1000){
                this.timeOut.run();
                alertTime=System.currentTimeMillis();
            }
            statusControl.accept(3,diff);
            return "时间已经超时: "+toHm(-diff/1000);
        }

        //  计算已经使用的番茄数量
        long currentHaveTime=getCurrentHaveWorkTime();
        if(currentHaveTime<workTime){
            long time=(workTime-currentHaveTime)/1000;
            if(time%10==0){
                remindMin.run();
            }
            statusControl.accept(1,time);
            String str=String.format("当前处于番茄工作状态  已经使用 %s 个番茄钟,正在使用第 %s 个番茄，该番茄剩余时间:%s",getUseCount(),getUseCount()+1,toHm(time));
            return str;
        }else{
            //  距离上一次提醒
            if((System.currentTimeMillis()-alertTime)>60*1000*3){
                remind.run();
                alertTime=System.currentTimeMillis();
            }
            long time=(stopTime-(currentHaveTime-workTime))/1000;
            /*if(time%10==0){
                remindMin.run();
            }*/
            remindMin.run();
            statusControl.accept(2,time);
            String str = String.format("当前处于番茄休息状态  已经使用 %s 个番茄钟,正在使用第 %s 个番茄，该番茄剩余时间:%s",getUseCount(),getUseCount()+1,toHm(time));
            return str;
        }




        /*if(diff/(1000*60)>15){
            //strBud.append(String.format("总体番茄个数%s 剩余番茄个数:%s 剩余分钟数:%s",getCount(),diff/(getWorkTime()+getStopTime()),(diff/(1000*60))+""));
            return String.format("总体番茄个数%s 剩余番茄个数:%s 剩余分钟数:%s",getCount(),diff/(getWorkTime()+getStopTime()),(diff/(1000*60))+"");
        }else {
            //strBud.append(String.format("总体番茄个数%s 剩余番茄个数:无 剩余分钟数:%s",getCount(),(diff/(1000*60))+""));
            return String.format("总体番茄个数%s 剩余番茄个数:无 剩余分钟数:%s",getCount(),(diff/(1000*60))+"");
        }*/
    }

    public String print2() {
        long endMill = this.endMill();
        long diff = endMill - Clock.toMill(LocalDateTime.now()) ;
        if(diff<0){
            return "时间已经超时: "+toHm(-diff/1000);
        }

        if(diff/(1000*60)>15){
            //strBud.append(String.format("总体番茄个数%s 剩余番茄个数:%s 剩余分钟数:%s",getCount(),diff/(getWorkTime()+getStopTime()),(diff/(1000*60))+""));
            return String.format("总体番茄个数%s 剩余番茄个数:%s 剩余分钟数:%s",getCount(),diff/(getWorkTime()+getStopTime()),(diff/(1000*60))+"");
        }else {
            //strBud.append(String.format("总体番茄个数%s 剩余番茄个数:无 剩余分钟数:%s",getCount(),(diff/(1000*60))+""));
            return String.format("总体番茄个数%s 剩余番茄个数:无 剩余分钟数:%s",getCount(),(diff/(1000*60))+"");
        }
    }

    public static String toHm(long second){
        return String.format("%02d:%02d",second/60,second%60);
    }

    private long getUnitTime(){
        return getWorkTime()+getStopTime();
    }

    //  得到已经使用了的事件
    private long getUseTime(){
        return Clock.toMill(LocalDateTime.now())-Clock.toMill(startTime);
    }

    //  得到使用了的番茄数量
    public long getUseCount(){
        //  计算已经使用了的番茄
        return (getUseTime()/getUnitTime());
    }

    //  得到当前番茄钟具有的已经工作时间
    private long getCurrentHaveWorkTime(){
        return getUseTime()-(getUseCount()*getUnitTime());
    }

}
